using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for ShopCategory
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class ShopCategory : SqlServerDAO
    {
        #region ShopCategory Private Fields

		private Int32 _id;
		private Int32 _shopId;
		private Int32 _categoryId;
		private Int32 _itemCount;
		private DateTime _createdOn;
		private DateTime _lastUpdate;
		private Int32 _totalView;

        #endregion

        #region ShopCategory Constractors

		public ShopCategory(){}

		public ShopCategory
		(
			Int32 id,
			Int32 shopId,
			Int32 categoryId,
			Int32 itemCount,
			DateTime createdOn,
			DateTime lastUpdate,
			Int32 totalView

		)
		{
			this._id = id;
			this._shopId = shopId;
			this._categoryId = categoryId;
			this._itemCount = itemCount;
			this._createdOn = createdOn;
			this._lastUpdate = lastUpdate;
			this._totalView = totalView;
		}

        #endregion

        #region ShopCategory Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public Int32 ShopId
		{
			get{ return this._shopId; }
			set{ this._shopId = value; }
		}

		public Int32 CategoryId
		{
			get{ return this._categoryId; }
			set{ this._categoryId = value; }
		}

		public Int32 ItemCount
		{
			get{ return this._itemCount; }
			set{ this._itemCount = value; }
		}

		public DateTime CreatedOn
		{
			get{ return this._createdOn; }
			set{ this._createdOn = value; }
		}

		public DateTime LastUpdate
		{
			get{ return this._lastUpdate; }
			set{ this._lastUpdate = value; }
		}

		public Int32 TotalView
		{
			get{ return this._totalView; }
			set{ this._totalView = value; }
		}


        #endregion

        #region ShopCategory Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewShopCategory
        (
			Int32 shopId,
			Int32 categoryId,
			Int32 itemCount,
			DateTime createdOn,
			DateTime lastUpdate,
			Int32 totalView

        )
        {
            //creating an empty ShopCategory object
            ShopCategory shopCategory = new ShopCategory();
            shopCategory.ShopId = shopId;
			shopCategory.CategoryId = categoryId;
			shopCategory.ItemCount = itemCount;
			shopCategory.CreatedOn = createdOn;
			shopCategory.LastUpdate = lastUpdate;
			shopCategory.TotalView = totalView;

            return ShopCategory.CreateNewShopCategory(shopCategory);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateShopCategory
        (
			Int32 id,
			Int32 shopId,
			Int32 categoryId,
			Int32 itemCount,
			DateTime createdOn,
			DateTime lastUpdate,
			Int32 totalView

        )
        {
            ShopCategory shopCategory = ShopCategory.GetShopCategoryById(id);
            shopCategory.ShopId = shopId;
			shopCategory.CategoryId = categoryId;
			shopCategory.ItemCount = itemCount;
			shopCategory.CreatedOn = createdOn;
			shopCategory.LastUpdate = lastUpdate;
			shopCategory.TotalView = totalView;

            return ShopCategory.UpdateShopCategory(shopCategory);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteShopCategory
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in ShopCategory"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_DELETESHOPCATEGORY);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteShopCategoryList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in ShopCategory"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_DELETESHOPCATEGORYLIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<ShopCategory> GetShopCategoryByShopId(Int32 shopId)
		{
			if(shopId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("shopId in ShopCategory"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@ShopId", SqlDbType.Int, 0, ParameterDirection.Input, shopId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_GETSHOPCATEGORYBYSHOPID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateShopCategoryCollectionFromReader);
			CustomCollection<ShopCategory> objCollection = ((CustomCollection<ShopCategory>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<ShopCategory> GetShopCategoryByCategoryId(Int32 categoryId)
		{
			if(categoryId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("categoryId in ShopCategory"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@CategoryId", SqlDbType.Int, 0, ParameterDirection.Input, categoryId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_GETSHOPCATEGORYBYCATEGORYID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateShopCategoryCollectionFromReader);
			CustomCollection<ShopCategory> objCollection = ((CustomCollection<ShopCategory>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

        #endregion

        #region ShopCategory Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewShopCategory(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateShopCategory(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<ShopCategory> GetAllShopCategory()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_GETALLSHOPCATEGORY);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateShopCategoryCollectionFromReader);
            CustomCollection<ShopCategory> objCollection = ((CustomCollection<ShopCategory>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<ShopCategory> GetAllShopCategoryPaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_GETALLSHOPCATEGORYPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateShopCategoryCollectionFromReader);
            CustomCollection<ShopCategory> objCollection = ((CustomCollection<ShopCategory>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllShopCategoryPagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_GETALLSHOPCATEGORYPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static ShopCategory GetShopCategoryById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in ShopCategory"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_GETSHOPCATEGORYBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateShopCategoryCollectionFromReader);
            CustomCollection<ShopCategory> objCollection = ((CustomCollection<ShopCategory>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (ShopCategory)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewShopCategory(ShopCategory newShopCategory)
        {
            // validate Parameters
            if (newShopCategory == null)
                throw (new ArgumentNullException("newShopCategory in ShopCategory"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@ShopId", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.ShopId);
			AddParameterToSqlCmd(sqlCmd, "@CategoryId", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.CategoryId);
			AddParameterToSqlCmd(sqlCmd, "@ItemCount", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.ItemCount);
			AddParameterToSqlCmd(sqlCmd, "@CreatedOn", SqlDbType.DateTime, 0, ParameterDirection.Input, newShopCategory.CreatedOn);
			AddParameterToSqlCmd(sqlCmd, "@LastUpdate", SqlDbType.DateTime, 0, ParameterDirection.Input, newShopCategory.LastUpdate);
			AddParameterToSqlCmd(sqlCmd, "@TotalView", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.TotalView);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_CREATENEWSHOPCATEGORY);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateShopCategory(ShopCategory newShopCategory)
        {
            // validate Parameters
            if (newShopCategory == null)
                throw (new ArgumentNullException("newShopCategory"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.Id);
            AddParameterToSqlCmd(sqlCmd, "@ShopId", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.ShopId);
			AddParameterToSqlCmd(sqlCmd, "@CategoryId", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.CategoryId);
			AddParameterToSqlCmd(sqlCmd, "@ItemCount", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.ItemCount);
			AddParameterToSqlCmd(sqlCmd, "@CreatedOn", SqlDbType.DateTime, 0, ParameterDirection.Input, newShopCategory.CreatedOn);
			AddParameterToSqlCmd(sqlCmd, "@LastUpdate", SqlDbType.DateTime, 0, ParameterDirection.Input, newShopCategory.LastUpdate);
			AddParameterToSqlCmd(sqlCmd, "@TotalView", SqlDbType.Int, 0, ParameterDirection.Input, newShopCategory.TotalView);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_SHOPCATEGORY_UPDATESHOPCATEGORY);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region ShopCategory Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of ShopCategory custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// ShopCategory information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateShopCategoryCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of ShopCategory collection
            CustomCollection<ShopCategory> colShopCategory = new CustomCollection<ShopCategory>();

            //Iterating thriugh the data reader, to generate ShopCategory collection.

            //getting the ShopCategory collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the ShopCategory constructor parameters from the current instance of data reader fields.
                ShopCategory newShopCategory = new ShopCategory
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["shop_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["shop_id"],
					returnData["category_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["category_id"],
					returnData["item_count"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["item_count"],
					returnData["created_on"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["created_on"],
					returnData["last_update"] == System.DBNull.Value ? DateTime.MinValue : (DateTime)returnData["last_update"],
					returnData["total_view"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["total_view"]

                );
                //adding the ShopCategory to the collection
                colShopCategory.Add(newShopCategory);
            }

            //returns the collection of ShopCategory 
            return (colShopCategory);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_SHOPCATEGORY_CREATENEWSHOPCATEGORY = "spr_ShopCategory_CreateNewShopCategory";
        internal const string SPR_SHOPCATEGORY_UPDATESHOPCATEGORY = "spr_ShopCategory_UpdateShopCategory";
        internal const string SPR_SHOPCATEGORY_DELETESHOPCATEGORY = "spr_ShopCategory_DeleteShopCategory";
        internal const string SPR_SHOPCATEGORY_DELETESHOPCATEGORYLIST = "spr_ShopCategory_DeleteShopCategoryList";
        internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYID = "spr_ShopCategory_GetShopCategoryById";
        internal const string SPR_SHOPCATEGORY_GETALLSHOPCATEGORY = "spr_ShopCategory_GetAllShopCategory";
        internal const string SPR_SHOPCATEGORY_GETALLSHOPCATEGORYPAGED = "spr_ShopCategory_GetAllShopCategoryPaged";
        internal const string SPR_SHOPCATEGORY_GETALLSHOPCATEGORYPAGEDCOUNT = "spr_ShopCategory_GetAllShopCategoryPagedCount";

		//Foreign key contants
		internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYSHOPID="spr_ShopCategory_GetShopCategoryByShopId";
		internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYSHOPIDPAGED="spr_ShopCategory_GetShopCategoryByShopIdPaged";
		internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYSHOPIDPAGEDCOUNT="spr_ShopCategory_GetShopCategoryByShopIdPagedCount";
		internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYCATEGORYID="spr_ShopCategory_GetShopCategoryByCategoryId";
		internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYCATEGORYIDPAGED="spr_ShopCategory_GetShopCategoryByCategoryIdPaged";
		internal const string SPR_SHOPCATEGORY_GETSHOPCATEGORYBYCATEGORYIDPAGEDCOUNT="spr_ShopCategory_GetShopCategoryByCategoryIdPagedCount";
		
        #endregion
    }
}
